package com.example.back.controller;

import com.example.back.service.AdminService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

@RestController
@RequestMapping("admin")
public class AdminController {
    private static final Logger logger = LogManager.getLogger(AdminController.class);

    @Resource
    private AdminService adminService;

    @RequestMapping("/login")
    public String adminLogin(String account, String password, HttpSession httpSession) {
        logger.info("account: " + account + "password: " + password);
        String rs = adminService.adminLogin(account, password, httpSession);
        if (rs.equals("成功")) rs = "location.href='';";
        if (rs.equals("失败")) rs = "alert('密码错误，登录失败!');";
        if (rs.equals("用户不存在")) rs = "alert('用户不存在!');";
        return rs;
    }

    @RequestMapping("/insertEmployee")
    public String insertEmployee(String name, int sex, Date birthday, String phone, String job) {
        String rs = "";
        if (adminService.insertEmployee(name, sex, birthday, phone, job)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>添加员工成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>添加员工失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/updateEmployeeJob")
    public String updateEmployeeJob(int employeeID, String job) {
        String rs = "";
        if (adminService.updateEmployeeJob(employeeID, job)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>更新员工成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>更新员工失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("/deleteEmployee")
    public String deleteEmployee(int employeeID) {
        String rs = "";
        if (adminService.deleteEmployee(employeeID)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除员工成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除员工失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("deleteRecordByID")
    public String deleteRecordByID(int id) {
        String rs;
        if (adminService.deleteRecordByID(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除记录成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>删除记录失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }

    @RequestMapping("updateRecordByID")
    public String updateRecordByID(int id) {
        String rs;
        if (adminService.updateRecordByID(id)) {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>该订单房间状态更新成功！点击返回（3秒后自动返回）</button>";
        } else {
            rs = "<button class='btn btn-light btn-sm' onClick='document.location.reload()'>该订单房间状态更新失败！点击返回（3秒后自动返回）</button>";
        }
        return rs;
    }
}
